<template>
  <div class="departments-container">
    <div class="app-container">
      <!-- 卡片组件(分为头部和身体) -->
      <el-card class="box-card">
        <div slot="header" class="clearfix">
          <!-- tabs标签导航页
            el-tabs (父容器) -> 一行标签页导航
               属性:
                v-model对应vue变量(保存的是选中选项卡的name值)->初始值也会影响选项卡默认选中第几个
              事件:
               tab-click 选项卡点击触发此事件方法
               (原理:我们组件内检测用户点击行为,回调tab-click事件)

               el-tabs-pane(子) -> 每个标签页导航(选项卡)
               属性: label -> 选项卡标题
               name -> 选项卡别名(标识,默认值从'1'开始)
               标签价夹着的是: tab导航切换时所属内容
           -->
          <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="组织架构" name="first">
              <!-- class="tab-pane"设置左右padding, 把内容往中间挤压 -->
              <!-- 使用 Layout 布局绘制头部区域
                最外层一行, 2列
                  el-row: type布局模式
                          justify两端对齐
                          align交叉轴排列方式(垂直方向居中)
               -->
              <!-- 头部 -->
              <el-row
                type="flex"
                justify="space-between"
                align="middle"
                class="department-header"
              >
                <!-- 第一列20份 -->
                <el-col :span="20">
                  <i class="el-icon-s-home" />
                  <span class="company">江苏传智播客教育科技股份有限公司</span>
                </el-col>
                <!-- 第二列4份 -->
                <el-col :span="4">
                  <!-- 在容器范围里, 一行再分2列 -->
                  <el-row type="flex" justify="end">
                    <el-col>负责人</el-col>
                    <el-col>
                      <!-- 这个之前在头部导航右侧下拉菜单讲过 -->
                      <el-dropdown>
                        <!-- 下拉菜单文字 -->
                        <span class="el-dropdown-link">
                          操作<i class="el-icon-arrow-down el-icon--right" />
                        </span>
                        <!-- 下拉项 -->
                        <el-dropdown-menu slot="dropdown">
                          <el-dropdown-item @click.native="add()">
                            添加子部门</el-dropdown-item
                          >
                        </el-dropdown-menu>
                      </el-dropdown>
                    </el-col>
                  </el-row>
                </el-col>
              </el-row>

              <!-- 列表
              知识点1:
              :
                 
                 *
                 data: 展示数据
                 props:配置选项
                 node-click	节点被点击时的回调
                 node-key: 每个树节点用来作为唯一标识的属性，整棵树应该是唯一的
                 default-expand-all: 是否默认展开所有的节点,默认就是false
                 expand-on-click-node: 是否在点击节点的时候展开或者收缩节点， 默认值为 true，如果为 false，则只有点箭头图标的时候才会展开或者收缩节点。
              -->
              <el-tree
                :data="treedata"
                :props="defaultProps"
                node-key="id"
                :default-expand-all="true"
                :expand-on-click-node="true"
                class="departments-tree"
                @node-click="handleNodeClick"
              >
                <!-- 默认插槽  并且使用内部的数据进行自定义模板的渲染
                  scope.data 所以直接key解构赋值变量也是data(对应数据对象)
                 -->
                <template #default="{ data }">
                  <el-row
                    type="flex"
                    justify="space-between"
                    align="middle"
                    style="height: 40px; width: 100%;"
                  >
                    <!-- 部门名字 -->
                    <el-col :span="20">
                      <span>{{ data.name }}</span>
                    </el-col>
                    <!-- 功能操作区域 -->
                    <el-col :span="4">
                      <el-row type="flex" justify="end">
                        <!-- 两个内容 -->
                        <el-col>{{ data.name }}</el-col>
                        <el-col>
                          <!-- 下拉菜单 element -->
                          <el-dropdown>
                            <span> 操作<i class="el-icon-arrow-down" /> </span>
                            <!-- 下拉菜单 -->
                            <el-dropdown-menu slot="dropdown">
                              <el-dropdown-item @click.native="add(data)"
                                >添加子部门</el-dropdown-item
                              >
                              <el-dropdown-item @click.native="edit(data)"
                                >编辑部门</el-dropdown-item
                              >
                              <el-dropdown-item
                                v-if="data && !data.children"
                                @click.native="del(data)"
                                >删除部门</el-dropdown-item
                              >
                            </el-dropdown-menu>
                          </el-dropdown>
                        </el-col>
                      </el-row>
                    </el-col>
                  </el-row>
                </template>
              </el-tree>
            </el-tab-pane>
          </el-tabs>
        </div>
      </el-card>
      <depart-dialog
        :dialog-visible.sync="dialogVisible"
        :sysUserSimpleList="sysUserSimpleList"
        @setForm="setForm"
        :origin-list="originList"
        :formID="formID"
        :isEdit="isEdit"
        ref="departDialog"
      ></depart-dialog>
      <!-- :departDialogObj="departDialogObj" -->
    </div>
  </div>
</template>

<script>
import {
  companyDepartment,
  sysUserSimple,
  addCompanyDepartment,
  getDepartDetailAPI,
  updateDepartmentsAPI,
  delDepartmentAPI
} from '@/api'
import { transTree } from '@/utils'
import departDialog from './components/departDialog.vue'
export default {
  components: {
    departDialog
  },
  data () {
    return {
      isEdit: false, // 新增 false,编辑true
      // departDialogObj: {},
      formID: '', // 点击行的id
      // getFormList: '', //子组件内传过来的form
      dialogVisible: false,
      activeName: 'first',
      sysUserSimpleList: [],
      treedata: [
        {
          departName: '总裁办',
          name: '孙财'
        },
        {
          departName: '行政部',
          name: '罗晓晓'
        },
        {
          departName: '人事部',
          name: '安小晴',
          children: [
            {
              departName: '财务核算部'
            }
          ]
        },
        {
          departName: '财务部',
          name: '',
          children: [
            {
              departName: '财务核算部'
            },
            {
              departName: '税务管理部'
            },
            {
              departName: '薪资管理部'
            }
          ]
        },
        {
          departName: '技术部',
          name: '',
          children: [
            {
              departName: '前端技术部'
            },
            {
              departName: '后端技术部'
            }
          ]
        },
        {
          departName: '运营部',
          name: ''
        },
        {
          departName: '市场部',
          name: '武高丽',
          children: [
            {
              departName: '市场部1'
            },
            {
              departName: '市场部2'
            }
          ]
        }
      ],
      defaultProps: {
        children: 'children', // children属性对应值,要对应数据对象哪个字段(来决定行节点是否有嵌套的子节点数据)
        label: 'departName' // label属性对应值,要对应数据对象里哪个字段(此对象字段值,决定节点行标签显示的中文)
      },
      originList: [] //部门列表数组,用于弹窗内-校验部门code和name是否重复数据数组
    }
  },
  created () {
    // 获取部门列表
    this.getcompanyDepartment()
    // 获取员工简单列表
    this.getsysUserSimple()
  },
  methods: {
    // 为了方便复用-请求逻辑,封装方法
    // 获取->部门列表
    async getcompanyDepartment () {
      const res = await companyDepartment()
      // 把扁平化数组里的对象,映射一份只有关键4对key+value值对象,形成一个新数组,用于传入表单组件做判断
      this.originList = res.data.depts.map(item => ({
        id: item.id,
        code: item.code,
        name: item.name,
        pid: item.pid
      }))
      // console.log(res)
      this.treedata = transTree(res.data.depts, '')
    },
    // 获取->员工简单列表
    async getsysUserSimple () {
      const res = await sysUserSimple()
      // console.log(res)
      this.sysUserSimpleList = res.data // 存到数组
    },
    //获取子组件的form
    async setForm (getFormList) {
      if (this.isEdit) {
        //编辑状态
        // getFormList.id = this.formID
        // console.log(formID)
        console.log(getFormList)
        const res = await updateDepartmentsAPI(getFormList)
        this.$message.success(res.message)
      } else {
        //添加
        getFormList.pid = this.formID
        await addCompanyDepartment(getFormList)
      }

      this.getcompanyDepartment()
    },
    // 选项卡点击事件
    handleNodeClick (data) {
      // console.log(data)
    },
    handleClick (tab, event) {
      console.log(tab, event)
    },
    // 正文部分-右侧的添加子部门
    async add (data) {
      if (data === undefined) {
        this.formID = ''
      } else {
        this.formID = data.id // 把点击行的部门id存入
      }
      // console.log(data)
      this.dialogVisible = true
      this.isEdit = false
      this.getcompanyDepartment()
    },
    fatherbtn () {
      this.dialogVisible = false
    },
    // 编辑子部分
    async edit (data) {
      this.formID = data.id // 把点击行的部门id存入

      this.dialogVisible = true
      this.isEdit = true //编辑的时候为true
      // this.formID = data.id

      const res = await getDepartDetailAPI(data.id)
      // this.departDialogObj = res.data
      this.$refs.departDialog.form = res.data
      console.log(res)
    },
    // 删除部分
    async del (data) {
      await delDepartmentAPI(data.id)
      this.$message.success('删除成功')
      this.getcompanyDepartment() //更新列表
    }
  }
}
</script>

<style lang="scss" scoped>
.departments-tree {
  margin: 12px 0px;
}
.box-card {
  padding: 10px 30px;
}

.tab-pane {
  padding: 20px 80px;

  .company {
    margin-left: 12px;
    font-weight: bold;
    font-size: 14px;
  }

  .department-header {
    height: 50px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid #ccc;
  }
}
</style>
